/* $Id:action_skill.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "action_skill"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "action_skill.h"
#include "skill.h"
#include "special.h"
#include "skill.h"
#include "special.h"
#include "../general/general.h"

action_skill::action_skill() {
    first_skill=0;
    first_special=0;
    second_skill=0;
    second_special=0;
    level=0;
}

// Field : first_skill

class skill& action_skill::getFirst_skill() const { if (first_skill==0) throw nullpointer(); return *first_skill; }
void action_skill::setFirst_skill(class skill &v) { first_skill=&v; }

// Field : first_special

class special& action_skill::getFirst_special() const { if (first_special==0) throw nullpointer(); return *first_special; }
void action_skill::setFirst_special(class special &v) { first_special=&v; }

// Field : second_skill

class skill& action_skill::getSecond_skill() const { if (second_skill==0) throw nullpointer(); return *second_skill; }
void action_skill::setSecond_skill(class skill &v) { second_skill=&v; }

// Field : second_special

class special& action_skill::getSecond_special() const { if (second_special==0) throw nullpointer(); return *second_special; }
void action_skill::setSecond_special(class special &v) { second_special=&v; }

// Field : level

const int action_skill::getLevel() const { return level; }
void action_skill::setLevel(const int v) { level=v; }

void action_skill::save(class channel &ch) {
    ch.write("action_skill", "");
    ch.write("f", getFirst_skill().getName());
    ch.write("fi", getFirst_special().getName());
    ch.write("s", getSecond_skill().getName());
    ch.write("se", getSecond_special().getName());
    ch.write("l", getLevel());
}

void action_skill::load(class channel &ch) {
    if (ch.key=="f" || ch.key=="first_skill") {
        if (!skill_repo.isSkill(ch.value)) {
            dbg_state("link unknown skill %s", ch.value.c_str());
            class skill rec;
            rec.setName(ch.value);
            skill_repo.setSkill(rec);
        }
        setFirst_skill(skill_repo.getSkill(ch.value));
        ch.next();
    }
    if (ch.key=="fi" || ch.key=="first_special") {
        if (!special_repo.isSpecial(ch.value)) {
            dbg_state("link unknown special %s", ch.value.c_str());
            class special rec;
            rec.setName(ch.value);
            special_repo.setSpecial(rec);
        }
        setFirst_special(special_repo.getSpecial(ch.value));
        ch.next();
    }
    if (ch.key=="s" || ch.key=="second_skill") {
        if (!skill_repo.isSkill(ch.value)) {
            dbg_state("link unknown skill %s", ch.value.c_str());
            class skill rec;
            rec.setName(ch.value);
            skill_repo.setSkill(rec);
        }
        setSecond_skill(skill_repo.getSkill(ch.value));
        ch.next();
    }
    if (ch.key=="se" || ch.key=="second_special") {
        if (!special_repo.isSpecial(ch.value)) {
            dbg_state("link unknown special %s", ch.value.c_str());
            class special rec;
            rec.setName(ch.value);
            special_repo.setSpecial(rec);
        }
        setSecond_special(special_repo.getSpecial(ch.value));
        ch.next();
    }
    if (ch.key=="l" || ch.key=="level") { setLevel(atoi(ch.value.c_str())); ch.next(); }
}

void action_skill::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<action_skill level=\"" << getLevel() <<
    "\">" << endl;
    if (r<2) try {
        getFirst_skill().xml(f,l+1,r+1);
        getFirst_special().xml(f,l+1,r+1);
        getSecond_skill().xml(f,l+1,r+1);
        getSecond_special().xml(f,l+1,r+1);
    } catch (nullpointer n) {}
    f << ncpy(l, "  ") << "</action_skill>" << endl;
}
